#include "tbfwidget.h"
#include "ui_tbfwidget.h"


TBFWidget::TBFWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TBFWidget)
{
    ui->setupUi(this);
    m_EffDmd = 0;
    m_DrawEffDmd = 0;
    m_xTracCmd = false;
    setEffDmd(false,true,false,80);
}

TBFWidget::~TBFWidget()
{
    delete ui;
}

void TBFWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.scale(this->width()/120.0,this->height()/400.0);
    drawScales(&painter);
    drawTBFValue(&painter);
}

void TBFWidget::drawScales(QPainter *painter)
{
    painter->save();
    painter->setPen(QPen(QPen(Qt::gray,1, Qt::SolidLine)));
    painter->setFont(QFont("Arial",11,QFont::Normal));
    painter->setBrush(Qt::transparent);
    painter->drawRect(10,40,30,320);//outer frame
    painter->drawLine(QPoint(10,190),QPoint(40,190));//coast pos
    painter->drawLine(QPoint(10,340),QPoint(40,340));//FB pos
    painter->drawLine(QPoint(40,40),QPoint(60,40));//+100%
    painter->drawLine(QPoint(40,190),QPoint(60,190));//0%
    painter->drawLine(QPoint(40,340),QPoint(60,340));//-100%
    painter->drawLine(QPoint(40,360),QPoint(60,360));//FB
    painter->setPen(QPen(QPen(Qt::white,1, Qt::SolidLine)));
    painter->drawText(QRect(62,25,70,30),Qt::AlignLeft|Qt::AlignVCenter,"T");
    painter->drawText(QRect(62,180,70,20),Qt::AlignLeft|Qt::AlignVCenter,"0");
    painter->drawText(QRect(62,325,70,30),Qt::AlignLeft|Qt::AlignVCenter,"B");
    painter->drawText(QRect(62,345,70,30),Qt::AlignLeft|Qt::AlignVCenter,"FB");
    painter->restore();
}

void TBFWidget::drawTBFValue(QPainter *painter)
{
    painter->save();
    painter->translate(25,190);
    painter->setPen(QPen(QPen(Qt::gray,1, Qt::SolidLine)));
    painter->setBrush(QColor(81,81,81));
    double indicatorInitX = -20;
    double indicatorInitY = (-1)*m_DrawEffDmd;
    if(m_xTracCmd)
        painter->setBrush(QColor(44,145,51));
    else
        painter->setBrush(QColor(255,255,255));
    painter->drawRect(-15,0,30,indicatorInitY);
    painter->setBrush(QColor(81,81,81));
    QPainterPath indicatePath;
    indicatePath.moveTo(indicatorInitX,indicatorInitY);
    indicatePath.lineTo(indicatorInitX+5,indicatorInitY-5);
    indicatePath.lineTo(indicatorInitX+35,indicatorInitY-5);
    indicatePath.lineTo(indicatorInitX+40,indicatorInitY);
    indicatePath.lineTo(indicatorInitX+35,indicatorInitY+5);
    indicatePath.lineTo(indicatorInitX+5,indicatorInitY+5);
    indicatePath.lineTo(indicatorInitX,indicatorInitY);
    painter->setPen(QPen(QPen(Qt::gray,1, Qt::SolidLine)));
    painter->setFont(QFont("Arial",11,QFont::Normal));
    painter->drawLine(QPoint(-15,300),QPoint(15,340));
    painter->setPen(QPen(QPen(Qt::white,1, Qt::SolidLine)));
    if(m_EffDmd == 0)
        painter->drawText(QRect(30,-75,70,30),Qt::AlignLeft|Qt::AlignVCenter,QString(""));
    else
        painter->drawText(QRect(30,m_xTracCmd?-75:75,70,30),
                          Qt::AlignLeft|Qt::AlignVCenter,
                          QString("%1%").arg(m_EffDmd));
    painter->drawPath(indicatePath);
    painter->restore();
}

void TBFWidget::setEffDmd(bool tracCmd, bool brkCmd, bool FBCmd, uint16_t value)
{
    m_EffDmd = value;
    if(FBCmd)
    {
        m_DrawEffDmd = -170;
        m_xTracCmd = false;
    }
    else if(brkCmd)
    {
        m_DrawEffDmd = value*(-15)/10;
        m_xTracCmd = false;
    }
    else if(tracCmd)
    {
        m_DrawEffDmd = value*15/10;
        m_xTracCmd = true;
    }
    else
    {
        m_DrawEffDmd = 0;
        m_xTracCmd = false;
    }
    update();
}
